import Vue from "vue";
import {Modal} from "ant-design-vue";
import ContextModal from '@/components/form/layout/ContextModal.vue';
import $ from 'jquery';

export const globalData = {
  h: undefined
};

export function createModelForVue() {
  return {
    prop: 'value',
    event: 'change'
  };
}

export function createProps(type, defaultValue) {
  if (type === String) return {type: String, default: defaultValue || undefined};
  if (type === Object) return {type: Object, default: defaultValue || (() => ({}))};
  if (type === Array) return {type: Array, default: defaultValue || (() => [])};
  if (type === Function) return {type: Function, default: defaultValue || undefined};
  if (type === Boolean) return {type: Boolean, default: defaultValue || false};
  if (type === Number) return {type: Number, default: defaultValue || 0};
}

export function randomUUID() {
  let chats = '0123456789abcdef';
  return randomString(32, chats);
}

export function randomString(length, chats) {
  if (!length) length = 1;
  if (!chats) chats = '0123456789qwertyuioplkjhgfdsazxcvbnm';
  let str = '';
  for (let i = 0; i < length; i++) {
    let num = randomNumber(0, chats.length - 1);
    str += chats[num];
  }
  return str;
}

export function randomNumber() {
  // 生成 最小值 到 最大值 区间的随机数
  const random = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1) + min);
  };
  if (arguments.length === 1) {
    let [length] = arguments;
    // 生成指定长度的随机数字，首位一定不是 0
    let nums = [...Array(length).keys()].map((i) => (i > 0 ? random(0, 9) : random(1, 9)));
    return parseInt(nums.join(''));
  } else if (arguments.length >= 2) {
    let [min, max] = arguments;
    return random(min, max);
  } else {
    return Number.NaN;
  }
}

export function openModal(vnode, config) {
  let div = document.createElement('div');
  document.body.appendChild(div);
  let component = Vue.component('test', {
    data() {
      return {
        visible: true,
      };
    },
    render(h) {
      return h(Modal, {
        props: {
          ...config,
          visible: this.visible,
        },
        on: {
          cancel: () => {
            this.close();
          },
          ok: () => {
            let {componentInstance} = vnode;
            if (componentInstance.check()) {
              this.close();
            }
          }
        }
      }, [vnode]);
    },
    methods: {
      close() {
        this.visible = false;
        this.$nextTick(() => {
          config.close && config.close();
        });
      },
    }
  });
  let comp = new Vue({
    render: () => globalData.h(component),
  }).$mount(div);
  return comp._vnode.componentInstance;
}

export function openContext({left, top}, context) {
  closeContext();
  let div = document.createElement('div');
  document.body.appendChild(div);
  $(div).addClass('david-context');
  let component = {
    render(h) {
      return h(ContextModal, {
        props: {
          context,
          left,
          top
        },
        on: {
          close: () => {
            closeContext();
          }
        }
      });
    }
  };
  new Vue({
    render: () => globalData.h(component),
  }).$mount(div);
}

function closeContext() {
  $('.david-context').remove();
}